package mall

import (
	"errors"
	"newbeemallStudyProject/global"
	"newbeemallStudyProject/model/mall/response"
	"newbeemallStudyProject/model/manage"
)

type MallIndexService struct{}

func (m *MallIndexService) QueryMallIndexConfigInfo(configType int, num int) (err error, list interface{}) {
	var indexConfig []manage.MallIndexConfig
	err = global.GVA_DB.Where("config_type = ?", configType).Where("is_deleted = 0").Order("config_rank desc").Limit(num).Find(&indexConfig).Error
	if err != nil {
		return errors.New("查询失败"), nil
	}
	//获取商品id
	var ids []int
	for _, indesid := range indexConfig {
		ids = append(ids, indesid.GoodsId)
	}
	//查询商品信息
	var mallGoodsList []manage.MallGoodsInfo
	err = global.GVA_DB.Where("goods_id in ?", ids).Find(&mallGoodsList).Error
	//if err != nil {
	//	return errors.New("查询商品详情失败"), nil
	//}
	var indexGoodsInexList []response.MallIndexConfigGoodsResponse
	for _, goodsInfo := range mallGoodsList {
		res := response.MallIndexConfigGoodsResponse{
			GoodsId:       goodsInfo.GoodsId,
			GoodsName:     goodsInfo.GoodsName,
			GoodsIntro:    goodsInfo.GoodsIntro,
			GoodsCoverImg: goodsInfo.GoodsCoverImg,
			SellingPrice:  goodsInfo.SellingPrice,
			Tag:           goodsInfo.Tag,
		}
		indexGoodsInexList = append(indexGoodsInexList, res)
	}
	return err, indexGoodsInexList
}
